home *** CD-ROM | disk | FTP | other *** search
- unit EventSink;
-
- interface
-
- uses Windows, ActiveX, Office_TLB;
-
- type
- TClickProc = procedure (const Ctrl: CommandBarButton;
- var CancelDefault: WordBool) of object;
-
- TEventSink = class(TObject, IUnknown, IDispatch)
- private
- FClickProc: TClickProc;
- { IUnknown }
- function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
- function _AddRef: Integer; stdcall;
- function _Release: Integer; stdcall;
- { IDispatch }
- function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
- function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
- function GetIDsOfNames(const IID: TGUID; Names: Pointer;
- NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
- function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
- Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
- public
- constructor Create(ClickProc: TClickProc);
- end;
-
- implementation
-
- { TEventSink }
-
- constructor TEventSink.Create(ClickProc: TClickProc);
- begin
- @FClickProc := @ClickProc;
- inherited Create;
- end;
-
- { TEventSink.IUnknown }
-
- function TEventSink._AddRef: Integer;
- begin
- // No need to implement, since lifetime is tied to add-in
- Result := 2;
- end;
-
- function TEventSink._Release: Integer;
- begin
- // No need to implement, since lifetime is tied to add-in
- Result := 1;
- end;
-
- function TEventSink.QueryInterface(const IID: TGUID; out Obj): HResult;
- begin
- // First look for my own implementation of an interface
- // (I implement IUnknown and IDispatch).
- if GetInterface(IID, Obj) then
- Result := S_OK
- // Next, if they are looking for outgoing interface, recurse to return
- // our IDispatch pointer.
- else if IsEqualIID(IID, DIID__CommandBarButtonEvents) then
- Result := QueryInterface(IDispatch, Obj)
- // For everything else, return an error.
- else
- Result := E_NOINTERFACE;
- end;
-
- { TEventSink.IDispatch }
-
- function TEventSink.GetIDsOfNames(const IID: TGUID; Names: Pointer;
- NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
- begin
- Result := E_NOTIMPL;
- end;
-
- function TEventSink.GetTypeInfo(Index, LocaleID: Integer;
- out TypeInfo): HResult;
- begin
- Pointer(TypeInfo) := nil;
- Result := E_NOTIMPL;
- end;
-
- function TEventSink.GetTypeInfoCount(out Count: Integer): HResult;
- begin
- Count := 0;
- Result := S_OK;
- end;
-
- function TEventSink.Invoke(DispID: Integer; const IID: TGUID;
- LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,
- ArgErr: Pointer): HResult;
- var
- DispParams: PVariantArgList;
- begin
- DispParams := TDispParams(Params).rgvarg;
- // Pass click event back to add-in
- if DispID = 1 then
- FClickProc(CommandBarButton(DispParams^[0].dispVal), DispParams^[1].pBool^);
- Result := S_OK;
- end;
-
- end.
-